#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>

// exce* 切换

// waitpid 和接收子进程退出信息
int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        // 子进程
        std::cout << "i am child process,my id: " << getpid() << std::endl;

        execlp("ls", "ls", "-a", "-l", nullptr);
        exit(1);
    }
    // 父进程
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
        if (WIFEXITED(status))
        {
            std::cout << "wait child: " << rid << " success" << "status code: " << WEXITSTATUS(status) << std::endl;
        }
        else
        {
            std::cout << " child quit fail" << std::endl;
        }
    }
    else
    {
        std::cerr << "wait error" << std::endl;
    }

    return 0;
}